package vn.edu.hut.soict.alignment;

public class AlignmentParser {
	private int[][] alignmentIndex;
	private String[][] alignmentString;
	private int destinationSize;

	public AlignmentParser(String alignmentCode) {
		destinationSize = 0;
		String[] split1 = alignmentCode.split(" ");
		alignmentIndex = new int[split1.length][];
		alignmentString = new String[split1.length][];
		for (int i = 0; i < split1.length; i++) {
			String[] split2 = split1[i].split("#");
			alignmentIndex[i] = new int[split2.length - 1];
			alignmentString[i] = new String[split2.length - 1];
			for (int j = 1; j < split2.length; j++) {
				String[] split3 = split2[j].split("\\|");
				alignmentIndex[i][j - 1] = Integer.parseInt(split3[0]);
				alignmentString[i][j - 1] = (split3.length > 1 ? split3[1] : "");
				if (alignmentIndex[i][j - 1] + 1 > destinationSize && alignmentString[i][j - 1] != "") {
					destinationSize = alignmentIndex[i][j - 1] + 1;
				}
			}
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < alignmentIndex.length; i++) {
			for (int j = 0; j < alignmentIndex[i].length; j++) {
				sb.append(alignmentIndex[i][j]).append(" ");
			}
			sb.append("# ");
		}
		sb.delete(sb.length() - 2, sb.length() - 1);
		return sb.toString();
	}

	public int[] alignmentIndex(int i) {
		if (i < alignmentIndex.length) {
			return alignmentIndex[i];
		}
		else {
			return null;
		}
	}
	
	public String[] alignmentString(int i) {
		if (i < alignmentString.length) {
			return alignmentString[i];
		}
		else {
			return null;
		}
	}
	
	public int destinationSize() {
		return destinationSize;
	}
}
